package com.xframe.xdal.core.model.sqlbuilder.proc;


import com.xframe.xdal.core.model.sqlbuilder.BaseSqlBuilder;
import com.xframe.xdal.core.model.sqlbuilder.BuilderContext;
import com.xframe.xdal.core.model.sqlbuilder.SqlExpression;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：根据唯一键(AK)判断记录是否存在
 * 创建人：xufeng
 * 创建时间：
 *
 * @version 1.0
 **/
public class ExistsByKeySqlBuilder extends BaseSqlBuilder {

    /**
     * sql 语句
     * @return
     */
    public String getSQL(){
        return "select count(1) from %s where %s";
    }

    @Override
    public SqlExpression build(BuilderContext context) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder builder = builderWhereByUniqueKey(context,paramList);
        String isExistSql = String.format(getSQL(),context.getNameConvert().toName(context.getTableInfo().getName()), builder);
        return new SqlExpression(isExistSql,paramList,context.getDataSourceTypeEnums());
    }
}
